/*
 * This file (AkToolsDlg.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package alkwarel.gui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import alkwarel.objects.AkArtspace;
import alkwarel.objects.AkBlender;
import alkwarel.objects.AkBrush;
import alkwarel.objects.AkFilter;
import alkwarel.objects.AkShape;
import artx.objects.AxObject;
import artx.utils.AxLog;

/**
 * Little tool selection interface.
 * 
 * @author Xavier Gouchet
 * 
 */
public class AkToolsDlg extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel mainPanel = null;

	private JComboBox shapeChoices = null;

	private JComboBox filterChoices = null;

	private JComboBox brushChoices = null;

	private JComboBox blenderChoices = null;

	private JButton shapeBtn = null;

	private JButton filterBtn = null;

	private JButton brushBtn = null;

	private JButton blenderBtn = null;

	private AkArtspace artspace = null;

	/**
	 * Standard constructor.
	 * 
	 * @param owner
	 *            the master window calling this dialog
	 * @param title
	 *            the title of the dialog
	 * @param artspace
	 *            the current artspace
	 */
	public AkToolsDlg(Frame owner, String title, AkArtspace artspace) {
		super(owner);
		this.artspace = artspace;
		this.setTitle(title);
		this.setContentPane(getMainPanel());
		this.pack();
	}

	private JPanel getMainPanel() {
		if (this.mainPanel == null) {
			this.mainPanel = new JPanel();
			this.mainPanel.setLayout(new GridBagLayout());

			GridBagConstraints gbc = new GridBagConstraints();

			gbc.insets = new Insets(2, 2, 2, 2);
			Dimension cbDim = new Dimension(150, 25);
			Dimension btnDim = new Dimension(75, 25);

			// Add Shapes
			gbc.gridy = 0;
			gbc.gridx = 0;
			gbc.gridwidth = 2;
			JLabel lbl = new JLabel("Shapes");
			this.mainPanel.add(lbl, gbc);

			gbc.gridy = 1;
			gbc.gridwidth = 1;
			this.shapeChoices = new JComboBox();
			this.FillShapes();
			this.shapeChoices.setPreferredSize(cbDim);
			this.mainPanel.add(this.shapeChoices, gbc);

			gbc.gridx = 1;
			this.shapeBtn = new JButton("Apply");
			this.shapeBtn.setPreferredSize(btnDim);
			this.shapeBtn.addActionListener(this);
			this.mainPanel.add(this.shapeBtn, gbc);

			// Add Filters
			gbc.gridy = 2;
			gbc.gridx = 0;
			gbc.gridwidth = 2;
			lbl = new JLabel("Filters");
			this.mainPanel.add(lbl, gbc);

			gbc.gridy = 3;
			gbc.gridwidth = 1;
			this.filterChoices = new JComboBox();
			this.FillFilters();
			this.filterChoices.setPreferredSize(cbDim);
			this.mainPanel.add(this.filterChoices, gbc);

			gbc.gridx = 1;
			this.filterBtn = new JButton("Apply");
			this.filterBtn.setPreferredSize(btnDim);
			this.filterBtn.addActionListener(this);
			this.mainPanel.add(this.filterBtn, gbc);

			// Add Brushes
			gbc.gridy = 4;
			gbc.gridx = 0;
			gbc.gridwidth = 2;
			lbl = new JLabel("Brushes");
			this.mainPanel.add(lbl, gbc);

			gbc.gridy = 5;
			gbc.gridwidth = 1;
			this.brushChoices = new JComboBox();
			this.FillBrushes();
			this.brushChoices.setPreferredSize(cbDim);
			this.mainPanel.add(this.brushChoices, gbc);

			gbc.gridx = 1;
			this.brushBtn = new JButton("Apply");
			this.brushBtn.setPreferredSize(btnDim);
			this.brushBtn.addActionListener(this);
			this.mainPanel.add(this.brushBtn, gbc);

			// Add Brushes
			gbc.gridy = 6;
			gbc.gridx = 0;
			gbc.gridwidth = 2;
			lbl = new JLabel("Blenders");
			this.mainPanel.add(lbl, gbc);

			gbc.gridy = 7;
			gbc.gridwidth = 1;
			this.blenderChoices = new JComboBox();
			this.FillBlenders();
			this.blenderChoices.setPreferredSize(cbDim);
			this.mainPanel.add(this.blenderChoices, gbc);

			gbc.gridx = 1;
			this.blenderBtn = new JButton("Apply");
			this.blenderBtn.setPreferredSize(btnDim);
			this.blenderBtn.addActionListener(this);
			this.mainPanel.add(this.blenderBtn, gbc);
		}
		return this.mainPanel;
	}

	private void FillShapes() {
		ArrayList<AxObject> objects = this.artspace.shapeManager.GetElements();
		for (int i = 0; i < objects.size(); i++) {
			AkShape shape = (AkShape) objects.get(i);
			if (shape != null) {
				this.shapeChoices.addItem(shape.GetName());
			}
		}
	}

	private void FillFilters() {
		ArrayList<AxObject> objects = this.artspace.filterManager.GetElements();
		for (int i = 0; i < objects.size(); i++) {
			AkFilter filter = (AkFilter) objects.get(i);
			if (filter != null) {
				this.filterChoices.addItem(filter.GetName());
			}
		}
	}

	private void FillBrushes() {
		ArrayList<AxObject> objects = this.artspace.brushManager.GetElements();
		for (int i = 0; i < objects.size(); i++) {
			AkBrush brush = (AkBrush) objects.get(i);
			if (brush != null) {
				this.brushChoices.addItem(brush.GetName());
			}
		}
	}

	private void FillBlenders() {
		ArrayList<AxObject> objects = this.artspace.blenderManager
				.GetElements();
		for (int i = 0; i < objects.size(); i++) {
			AkBlender blender = (AkBlender) objects.get(i);
			if (blender != null) {
				this.blenderChoices.addItem(blender.GetName());
			}
		}
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.shapeBtn) {
			String cmd = this.shapeChoices.getSelectedItem().toString();
			AxLog.Debug("AkToolsDlg::actionPerformed =>> Applying shape \""
					+ cmd + "\"");
			this.artspace.AppendInstruction(cmd, "shape");
		}

		if (e.getSource() == this.filterBtn) {
			String cmd = this.filterChoices.getSelectedItem().toString();
			AxLog.Debug("AkToolsDlg::actionPerformed =>> Applying filter \""
					+ cmd + "\"");
			this.artspace.AppendInstruction(cmd, "filter");
		}

		if (e.getSource() == this.brushBtn) {
			String cmd = this.brushChoices.getSelectedItem().toString();
			AxLog.Debug("AkToolsDlg::actionPerformed =>> Setting brush \""
					+ cmd + "\"");
			this.artspace.SelectBrush(cmd);
		}

		if (e.getSource() == this.blenderBtn) {
			String cmd = this.blenderChoices.getSelectedItem().toString();
			AxLog.Debug("AkToolsDlg::actionPerformed =>> Setting blender \""
					+ cmd + "\"");
			this.artspace.SelectBlender(cmd);
		}
	}
}
